package com.songcha.module_vip.ui.activity.vip_recharge

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.beanFlatMap
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.viewmodel.BaseErrorViewModel
import com.songcha.module_vip.bean.vip.CreateAlipayVipRechargeOrderDataBean
import com.songcha.module_vip.bean.vip.CreateWxVipRechargeOrderDataBean
import com.songcha.module_vip.bean.vip.VipRechargePriceDataBean

class VipRechargeViewModel(app: Application):BaseErrorViewModel<VipRechargeRepository>(app) {
    var createWxVipRechargeOrderBean=MutableLiveData<CreateWxVipRechargeOrderDataBean.CreateWxVipRechargeOrderPayBean.CreateWxVipRechargeOrderBean>()
    var createAlipayVipRechargeOrderInfo=MutableLiveData<CreateAlipayVipRechargeOrderDataBean.CreateAlipayVipRechargeOrderPayBean.CreateAlipayVipRechargeOrderBean>()
    var createVipRechargeOrderState=MutableLiveData<Boolean>()
    var vipRechargePriceBean=MutableLiveData<VipRechargePriceDataBean.VipRechargePriceBean>()
    //paytype：1wx，2alipay
    var payType=MutableLiveData(1)
    var isPaying=MutableLiveData(false)
    //var currentVipPriceBean=MutableLiveData<VipRechargePriceDataBean.VipRechargePriceBean.OpenVipBean>()
    var currentVipPrice=MutableLiveData<Double>()
    var orderId:String=""
    var orderPayStatus=MutableLiveData<Int>()

    fun setPayType(type:Int){
        payType.value=type
    }

    fun setIsPaying(paying:Boolean){
        isPaying.value=paying
    }

    fun setCurrentPrice(position:Int){
        if(vipRechargePriceBean.value==null) return
        if(position>vipRechargePriceBean.value!!.openVipList.size-1) return
        if(vipRechargePriceBean.value!!.first && vipRechargePriceBean.value!!.openVipList[position].firstMoney>0f){
            currentVipPrice.value=vipRechargePriceBean.value!!.openVipList[position].firstMoney
        }else{
            currentVipPrice.value=vipRechargePriceBean.value!!.openVipList[position].ordinaryMoney
        }
    }

    fun getVipRechargePriceList(){
        handleApiDataObserver(repository!!.getVipRechargePriceList(),object :
            IApiResult<VipRechargePriceDataBean> {
            override fun onSuccess(bean: VipRechargePriceDataBean) {
                vipRechargePriceBean.value=bean.data
                if(bean.data.openVipList.size>0){
                    currentVipPrice.value=bean.data.openVipList[0].ordinaryMoney
                    if(bean.data.first && bean.data.openVipList[0].firstMoney>0f){
                        currentVipPrice.value=bean.data.openVipList[0].firstMoney
                    }else{
                        currentVipPrice.value=bean.data.openVipList[0].ordinaryMoney
                    }
                    //currentVipPriceBean.value=bean.data.openVipList[0]
                }
            }

            override fun onError(e: Throwable) {

            }

        },true)
    }

    fun createVipRechargeOrderWx(id:Int){
        isPaying.value=true
        handleApiDataObserver(repository!!.createVipRechargeOrderWx(id),object :IApiResult<CreateWxVipRechargeOrderDataBean>{
            override fun onSuccess(bean: CreateWxVipRechargeOrderDataBean) {
                createWxVipRechargeOrderBean.value=bean.data.wxPay
                orderId=bean.data.orderId.toString()
                createVipRechargeOrderState.value=true
            }

            override fun onError(e: Throwable) {
                createVipRechargeOrderState.value=false
                isPaying.value=false
            }

        })
    }

    fun createVipRechargeOrderAlipay(id:Int){
        isPaying.value=true
        handleApiDataObserver(repository!!.createVipRechargeOrderAlipay(id),object :IApiResult<CreateAlipayVipRechargeOrderDataBean>{
            override fun onSuccess(bean: CreateAlipayVipRechargeOrderDataBean) {
                createAlipayVipRechargeOrderInfo.value=bean.data.aliPay
                orderId=bean.data.orderId.toString()
                createVipRechargeOrderState.value=true
            }

            override fun onError(e: Throwable) {
                createVipRechargeOrderState.value=false
                isPaying.value=false
            }

        })
    }

    fun queryVipRechargeOrderState(){
        if(orderId.isBlank()) return
        var payStatus=0
        val obs=repository!!.queryVipRechargeOrderState(orderId)
            .beanFlatMap {
                payStatus=it.data
                return@beanFlatMap repository!!.getUserInfo()
            }

        handleApiDataObserver(obs,object :IApiResult<UserInfoBean>{
            override fun onSuccess(bean: UserInfoBean) {
                UserInfoManager.saveUserInfo(bean.data)
                orderPayStatus.value=payStatus
                isPaying.value=false
            }

            override fun onError(e: Throwable) {
                isPaying.value=false
            }

        })
    }

}